﻿using Client.Constant;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Google.Protobuf;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Forms;

namespace Client.ViewModel.CommonViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: HomePageViewModel
    /// CreateTime: 2020/6/9 16:26:26
    /// </summary>
    public class HomePageViewModel : BindableBase
    {
        public HomePageViewModel()
        {
            MsgEventSubscribe();
            Init();

            //TestSendFile();
        }

        private void Init()
        {
            InitTimer();
            RequestLoginLogs();
        }

        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.RESPONSE_LOGIN_LOG, ResponseLoginLogs);
        }

        private void RequestLoginLogs()
        {
            LoginLogReq req = new LoginLogReq(MessageId.REQUEST_LOGIN_LOG);
            ApplicationClient.Instance().Send(req);
        }

        public System.Timers.Timer LoginTimer { get; set; }

        public TimeSpan Span { get; set; }

        private void InitTimer()
        {
            LoginTimer = new System.Timers.Timer(1000);
            LoginTimer.AutoReset = true;
            LoginTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerTick);
        }

        private void TimerTick(object sender, System.Timers.ElapsedEventArgs e)
        {
            Span = DateTime.Now - LoginTime;
            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                OnLineTimeSpan = string.Format("{0:00}:{1:00}:{2:00}", Span.Hours, Span.Minutes, Span.Seconds);
            }));
        }

        /// <summary>
        /// 上次登录时间
        /// </summary>
        private string lastLoginTime = "暂无";

        public string LastLoginTime
        {
            get { return lastLoginTime; }
            set
            {
                lastLoginTime = value;
                RaisePropertyChanged("LastLoginTime");
            }
        }

        /// <summary>
        /// 上次登录IP
        /// </summary>
        private string lastLoginIp = "暂无";

        public string LastLoginIp
        {
            get { return lastLoginIp; }
            set
            {
                lastLoginIp = value;
                RaisePropertyChanged("LastLoginIp");
            }
        }



        /// <summary>
        /// 在线时长
        /// </summary>
        private string onlineTimeSpan = string.Empty;

        public string OnLineTimeSpan
        {
            get { return onlineTimeSpan; }
            set
            {
                onlineTimeSpan = value;
                RaisePropertyChanged("OnLineTimeSpan");
            }
        }


        public DateTime LoginTime { get; set; }

        private void ResponseLoginLogs(ResponseBaseMessage message)
        {
            if (message is LoginLogResp msg)
            {
                string loginTimeStr = msg.Logs.First().LoginTime;
                DateTimeFormatInfo format = new DateTimeFormatInfo();
                format.ShortDatePattern = "yyyy/MM/dd hh:mm:ss";
                LoginTime = Convert.ToDateTime(loginTimeStr, format);
                LoginTimer.Start();
                if (msg.Logs.Count > 2)
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        LastLoginTime = msg.Logs[1].LoginTime;
                        LastLoginIp = msg.Logs[1].LoginIp;
                    }));
                }
            }
        }







        private void TestSendFile()
        {
            using (FileStream fs = new FileStream(@"C:\Users\leil1\Desktop\新建文本文档.txt", FileMode.Open))
            {
                int len = (int)fs.Length;
                byte[] bytes = new byte[len];
                int res = fs.Read(bytes, 0, len);
                ByteString data = ByteString.CopyFrom(bytes);
                File file = new File();
                file.FileName = fs.Name;
                file.Size = len;
                file.Data = data;
                FileSendReq req = new FileSendReq(MessageId.SEND_FILE);
                req.File = file;
                ApplicationClient.Instance().Send(req);
            }
        }









    }
}
